local Player = {}
local Screenwide = ScrW()
local Screenheigh = ScrH()
local Xstartposition = 0
local Ystartposition = Screenheigh/8*7
local Wide = Screenwide
local Heigh = Screenheigh/8
local Fontheigh = 16
local xhealthbarstart = Screenwide/2
local yhealthbarstart = Ystartposition+Fontheigh
local healthbarheigh = Heigh-Fontheigh 
local healthbarwide = 100
local xressourcebarstartlinks = xhealthbarstart+healthbarwide-40
local ressourcebarwide = Screenwide - xressourcebarstartlinks -10
local xressourcebarstartrechts = xressourcebarstartlinks+ressourcebarwide/4*2 +5
local ressourcebarheigh = Heigh/4
local yressourcebarstartline1 = Ystartposition +2.5
local yressourcebarstartline2 = yressourcebarstartline1 + ressourcebarheigh
local yressourcebarstartline3 = yressourcebarstartline2 + ressourcebarheigh
local yressourcebarstartline4 = yressourcebarstartline3 + ressourcebarheigh
local ressourcebarsingelheigh = ressourcebarheigh -4
local ystatusline1 = yressourcebarstartline2
local ystatusline2 = yressourcebarstartline1 + ressourcebarheigh * 1.6
local ystatusline3 =  yressourcebarstartline1 + ressourcebarheigh * 2.2
local ystatusline4 = yressourcebarstartline1 + ressourcebarheigh * 2.8
local xtextcenterlinks = xressourcebarstartlinks+ressourcebarwide/4
local xtextcenterrechts = xressourcebarstartlinks+ressourcebarwide/4*3	
local xstatusbarstart = 10
local statusbarwide = xhealthbarstart - 70
local statusbarheigh = Heigh-Fontheigh 
local xtextcenterstatusbar = xstatusbarstart + statusbarwide/4
local yshieldstart = yressourcebarstartline2 +2
local yshieldheigh = ressourcebarheigh*2 -4
local ystructurestart = yressourcebarstartline2 + ressourcebarheigh/2
local xstructurestart = xstatusbarstart+10
local structureheigh = yshieldheigh-20
local structurewide = statusbarwide-20
local ytextline1 = yressourcebarstartline2 + ressourcebarheigh/6
local ytextline2 = yressourcebarstartline3 + ressourcebarheigh/6
local shieldcolour = false
local YSpeedbarstart = 10 
local Yspeedbarheigh = Screenheigh/8*7 
local XSpeedbarstart = 5
local XSpeedbarend = 20
local Textcahe = {}



local radarsize = ScrH()/3.5
local nX = (ScrW() - radarsize)+(radarsize/2)
local nY = Ystartposition-(radarsize/2)
local radarboxxstart = nX-(radarsize/2) 
local radarboxystart = nY-(radarsize/2)
local aPoints = {}
local aPointsb = {}
local nMapPos = 0
local aMapPoints = {}

local song = ""
GUIToggled = false;
HelpToggled = false;
HelpLabels = { }
HelpCategories = { }
AdminTellAlpha = -1;
AdminTellStartTime = 0;
AdminTellMsg = "";
surface.CreateFont("Tahoma", 10, 800, true, false, "RadarClassNames")
surface.CreateFont("Tahoma", 12, 800, true, false, "RadarCompass")

local MOTD = {}

Player.Air = 0
Player.MaxAir = 1
Player.Coolant = 0
Player.MaxCoolant = 1
Player.Energy = 0
Player.MaxEnergy = 1
Player.Heat = 0
Player.MaxHeat = 1
Player.Bullets = 0
Player.MaxBullets = 1
Player.Rockets = 0
Player.MaxRockets = 1
Player.Techlevel = 0
Player.Shield = 0
Player.MaxShield = 1
Player.CoreHealth = 0
Player.CoreMaxHealth = 1
Player.Turrets = 0
Player.MaxTurrets = 1
Player.Speed = 0
Player.Capaciator = 0
Player.MaxCapaciator = 1
Player.Cpu = 0
Player.MaxCpu = 1
Player.InVehicle2 = false
menu = {}
hudline = 1

function GM:Initialize()
	self.BaseClass:Initialize()
end

function GM:HUDPaint()
	self:PaintWorldTips()
	self.BaseClass:HUDPaint()
	self:PaintNotes()
	draw.DrawText("Status", "ScoreboardText",Screenwide/2 ,Ystartposition, Color(255,255,255,255),1)
	draw.RoundedBox( 3,xhealthbarstart-50,yhealthbarstart,healthbarwide,healthbarheigh, Color(00,50,00,100) )
	draw.DrawText("Health ".. LocalPlayer():Health() , "ScoreboardText",xhealthbarstart,ystatusline1, Color(255,255,255,255),1)
	if(LocalPlayer():GetActiveWeapon() != NULL) then
		local ammo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
		if (ammo != -1) then 
			draw.DrawText("Ammo: ".. ammo  , "ScoreboardText",xhealthbarstart,ystatusline2, Color(255,255,255,255),1)
		end
	end
	draw.DrawText("Rep: " .. Player.Reputation , "ScoreboardText",xhealthbarstart,ystatusline3, Color(255,255,255,255),1)
	draw.DrawText("$" .. Player.Money , "ScoreboardText",xhealthbarstart,ystatusline4, Color(255,255,255,255),1)
		if not Player.InVehicle2 then return end
		local Speed = Player.Speed / 500 * Yspeedbarheigh
		draw.RoundedBox( 0,XSpeedbarstart,Yspeedbarheigh - Speed,XSpeedbarend,Speed, Color(150,30,30,80) ) 
		draw.RoundedBox( 0,Xstartposition,Ystartposition,Wide,Heigh, Color(50,50,75,60) ) 
		local  Cpu = Player.Cpu / Player.MaxCpu * ressourcebarwide/2
		local  Coolant = Player.Coolant / Player.MaxCoolant * ressourcebarwide/2
		local  Heat = Player.Heat / 100 * ressourcebarwide/2
		local  Energy = Player.Energy / Player.MaxEnergy * ressourcebarwide/2
		local  Bullets = Player.Bullets / Player.MaxBullets * ressourcebarwide/2
		local  Rockets = Player.Rockets / Player.MaxRockets * ressourcebarwide/2
		local  Capaciator = Player.Capaciator / Player.MaxCapaciator * ressourcebarwide/2		
		draw.RoundedBox( 0,xressourcebarstartlinks,yressourcebarstartline1,Cpu,ressourcebarsingelheigh, Color(26,60,255,150) )
		draw.DrawText("Cpu usage: " .. Player.Cpu .." / ".. Player.MaxCpu, "ScoreboardText",xtextcenterlinks,yressourcebarstartline1, Color(50,100,255,255),1)
		--draw.RoundedBox( 0,xressourcebarstartlinks,yressourcebarstartline2,Coolant,ressourcebarsingelheigh, Color(0,0,246,150) )
		--draw.DrawText("Coolant: " .. Player.Coolant .." / ".. Player.MaxCoolant, "ScoreboardText",xtextcenterlinks,yressourcebarstartline2, Color(50,50,255,255),1)
		draw.RoundedBox( 0,xressourcebarstartlinks,yressourcebarstartline3,Heat,ressourcebarsingelheigh, Color(220,0,0,150) )
		draw.DrawText("Heat: " .. Player.Heat .." / ".. 100, "ScoreboardText",xtextcenterlinks,yressourcebarstartline3, Color(255,30,30,255),1)
		draw.RoundedBox( 0,xressourcebarstartlinks,yressourcebarstartline4,Energy,ressourcebarsingelheigh, Color(220,210,0,150) )
		draw.DrawText("Energy: " .. Player.Energy .." / ".. Player.MaxEnergy, "ScoreboardText",xtextcenterlinks,yressourcebarstartline4, Color(220,190,30,255),1)
		draw.RoundedBox( 0,xressourcebarstartrechts,yressourcebarstartline1,Bullets,ressourcebarsingelheigh, Color(220,210,0,150) )
		draw.DrawText("Bullets: " .. Player.Bullets .." / ".. Player.MaxBullets, "ScoreboardText",xtextcenterrechts,yressourcebarstartline1, Color(220,190,30,255),1)
		draw.RoundedBox( 0,xressourcebarstartrechts,yressourcebarstartline2,Rockets,ressourcebarsingelheigh, Color(220,210,0,150) )
		draw.DrawText("Rockets: " .. Player.Rockets .." / ".. Player.MaxRockets, "ScoreboardText",xtextcenterrechts,yressourcebarstartline2, Color(220,190,30,255),1)
		draw.RoundedBox( 0,xressourcebarstartrechts,yressourcebarstartline3,Capaciator,ressourcebarsingelheigh, Color(220,210,0,150) )
		draw.DrawText("Capaciator: " .. Player.Capaciator .." / ".. Player.MaxCapaciator, "ScoreboardText",xtextcenterrechts,yressourcebarstartline3, Color(220,190,30,255),1)
		local Shield = Player.Shield / Player.MaxShield * statusbarwide
		if (Player.Shield / Player.MaxShield < 0.3) then
			if (shieldcolour) then 
				draw.RoundedBox( 2,xstatusbarstart,yshieldstart,Shield,yshieldheigh, Color(255,0,0,255)  )
				shieldcolour = false
			else
				draw.RoundedBox( 2,xstatusbarstart,yshieldstart,Shield,yshieldheigh, Color(100,0,255,255)  )
				shieldcolour = true
			end
		else
			draw.RoundedBox( 2,xstatusbarstart,yshieldstart,Shield,yshieldheigh, Color(100,0,255,255)  )
		end
		local Structure = Player.CoreHealth / Player.CoreMaxHealth * structurewide
		draw.RoundedBox( 2,xstructurestart,ystructurestart,Structure,structureheigh, Color(220,100,100,255) )
		draw.DrawText("Shield: " .. Player.Shield .." / ".. Player.MaxShield, "ScoreboardText",xtextcenterstatusbar,ytextline1, Color(26,60,255,150),1)
		draw.DrawText("Structure: " .. Player.CoreHealth .." / ".. Player.CoreMaxHealth, "ScoreboardText",xtextcenterstatusbar,ytextline2, Color(150,0,0,150) ,1)
		draw.DrawText("Techlevel: " .. Player.Techlevel .. " Turrets " .. Player.Turrets .. "/".. Player.MaxTurrets .. "Sprint for Autofly,Reload for Turretmode", "ScoreboardText",xstructurestart,yressourcebarstartline1, Color(200,200,200,255),0)
		local client = LocalPlayer()
		if !client:Alive() then return end
		local friendlies = {"armourcharger","healthcharger"}
		for k,v in pairs(friendlies) do
			addEnt(v, Color(0, 255, 0, 127), 1)
		end
		local enemies = {"laser_blaster_1000_bullet"}
		for k,v in pairs(enemies) do
			addEnt(v, Color(255, 0, 0, 127), 0.5)
		end
		local bigenemies = {"npc_Enemy1","mine"}
		for k,v in pairs(bigenemies) do
			addEnt(v, Color(255, 0, 0, 127), 1)
		end
		local useful = {"Test"}
		for k,v in pairs(useful) do
			addEnt(v, Color(255, 255, 0, 127), 1)
		end
		local biguseful = {"Test2"}
		for k,v in pairs(biguseful) do
			addEnt(v, Color(255, 255, 0, 127), 2)
		end
		addPlayer(Color(127, 127, 255, 127), 1)
		vAim = client:GetAimVector():Angle()
		nXRot = -vAim.p
		nYRot = -vAim.y
		draw.RoundedBox( 0,radarboxxstart,radarboxystart, radarsize, radarsize, Color(50,50,75,60) )
		for k,v in pairs(aPoints) do
			calculateAndDraw3D(v[0], v[1], v[2], nXRot, nYRot, v[3], v[4], v[5], v[6], v[7])
			if(k == 3) then
				calculateCompass3D(v[0], v[1], v[2], nXRot, nYRot, "S")
			end
			if(k == 6) then
				calculateCompass3D(v[0], v[1], v[2], nXRot, nYRot, "E")
			end
			if(k == 9) then
				calculateCompass3D(v[0], v[1], v[2], nXRot, nYRot, "N")
			end
			if(k == 0) then
				calculateCompass3D(v[0], v[1], v[2], nXRot, nYRot, "W")
			end
		end
		for k,v in pairs(aPointsb) do
			calculateAndDraw3D(v[0], v[1], v[2], nXRot, nYRot, v[3], v[4], v[5], v[6], v[7])
		end
		aPointsb = {}

end

function GM:HUDShouldDraw( name )
	if( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo" ) then return false; end
	if( HelpToggled and name == "CHudChat" ) then return false; end
	return true;
end

function shipcorehud( um ) --getting the stuf from ship core
 	Player.Energy		= um:ReadLong()
	Player.MaxEnergy	= um:ReadLong()
	Player.Heat			= um:ReadLong()
	Player.Bullets		= um:ReadLong()
	Player.MaxBullets 	= um:ReadLong()
	Player.Rockets		= um:ReadLong()
	Player.MaxRockets 	= um:ReadLong()
	Player.Shield	 	= um:ReadLong()
	Player.MaxShield	= um:ReadLong()
	Player.CoreHealth	= um:ReadLong()
	Player.CoreMaxHealth= um:ReadLong()
	Player.Turrets		= um:ReadLong()
	Player.MaxTurrets	= um:ReadLong()
	Player.Capaciator		= um:ReadLong()
	Player.MaxCapaciator	= um:ReadLong()
	Player.Cpu		= um:ReadLong()
	Player.MaxCpu	= um:ReadLong()
end 
 usermessage.Hook("Hud-Shipcore", shipcorehud)  

function Techlevel( um )
    Player.Techlevel 	= um:ReadLong()
end 
usermessage.Hook("Techlevel", Techlevel)
 

function Moneychange( um )
    Player.Money = um:ReadLong()
end 
usermessage.Hook("MoneyChange", Moneychange) 

function Speedchange( um )
    Player.Speed = um:ReadLong()
end 
usermessage.Hook("SpeedChange", Speedchange) 

 
function ReputationChange( um )
    local rep = um:ReadLong()
	if (rep == 1) then Player.Reputation = "Good" end
	if (rep == 2) then Player.Reputation = "Bad" end
	if (rep == 3) then Player.Reputation = "Trader" end
end 
usermessage.Hook("ReputationChange", ReputationChange) 
 
function drawLine(nStartX, nStartY, nStartZ, nEndX, nEndY, nEndZ, nYRot, nXRot)
	local nExp = 220 -- Exposure, unknown unit
	local nYRotRad = nYRot/180*math.pi
	local nCosY = math.cos(nYRotRad)
	local nSinY = math.sin(nYRotRad)
	local nXRotRad = nXRot/180*math.pi
	local nCosX = math.cos(nXRotRad)
	local nSinX = math.sin(nXRotRad)
	local nStartX2 = nStartX*nCosY-nStartZ*nSinY
	local nStartZ2 = nStartZ*nCosY+nStartX*nSinY
	local nStartY2 = nStartY*nCosX-nStartZ2*nSinX
	local nStartZ3 = nStartZ2*nCosX+nStartY*nSinX
	local nEndX2 = nEndX*nCosY-nEndZ*nSinY
	local nEndZ2 = nEndZ*nCosY+nEndX*nSinY
	local nEndY2 = nEndY*nCosX-nEndZ2*nSinX
	local nEndZ3 = nEndZ2*nCosX+nEndY*nSinX
	local nStartScale = (nExp/(nExp+nStartZ3))*(radarsize/240)
	local nStartScrPosX = nX+nStartX2*nStartScale
	local nStartScrPosY = nY-nStartY2*nStartScale
	local nEndScale = (nExp/(nExp+nEndZ3))*(radarsize/240)
	local nEndScrPosX = nX+nEndX2*nEndScale
	local nEndScrPosY = nY-nEndY2*nEndScale
	surface.SetDrawColor(255, 255, 255, 127)
	surface.DrawLine(nStartScrPosX, nStartScrPosY, nEndScrPosX, nEndScrPosY)
end

function getSpriteId(sClass)
	return surface.GetTextureID("radaricons/"..sClass)
end

function getEntName(sClass)
	local nLength = string.len(sClass)
	local nStart = 0
	for var=0, nLength-1, 1 do
		if(string.sub(sClass, var, var) == "_") then
			nStart = var+1
		end
	end
	return string.sub(sClass, nStart, nLength)
end

local nCompassRadius = 90

for i=0, 11, 1 do
	local nRad = (i*30)/180*math.pi
	local cDotColour = Color(200,200,200, 127)
	if(i == 9) then cDotColour = Color(200,200,255, 127) end
	aPoints[i] = {}
	aPoints[i][0] = math.cos(nRad)*nCompassRadius
	aPoints[i][1] = 0
	aPoints[i][2] = math.sin(nRad)*nCompassRadius
	aPoints[i][3] = cDotColour
	aPoints[i][4] = false
	aPoints[i][5] = 0.7
	aPoints[i][6] = "compass"
	aPoints[i][7] = "compass"
	if(i == 9) then aPoints[i][5] = 1.1 end
end

function calculateAndDraw3D(nXPos, nYPos, nZPos, nXRot, nYRot, cColour, bRestrictRadius, nSizeOffs, sClassName, sIconName)
	local nExp = 220 -- Exposure, unknown unit
	local nDotSize = 3
	local nMaxDist = 80
	local nXZDist = math.sqrt(nXPos*nXPos+nZPos*nZPos)
	if(nXZDist>nMaxDist && bRestrictRadius) then
		return
	end
	
	local nYRotRad = (nYRot-90)/180*math.pi
	local nCosY = math.cos(nYRotRad)
	local nSinY = math.sin(nYRotRad)
	local nXPos2 = nXPos*nCosY-nZPos*nSinY
	local nZPos2 = nZPos*nCosY+nXPos*nSinY
	local nXRotRad = nXRot/180*math.pi
	local nCosX = math.cos(nXRotRad)
	local nSinX = math.sin(nXRotRad)
	local nYPos2 = nYPos*nCosX-nZPos2*nSinX
	local nZPos3 = nZPos2*nCosX+nYPos*nSinX
	
	local nScale = (nExp/(nExp+nZPos3))*(radarsize/240)
	local nScrPosX = nX+nXPos2*nScale
	local nScrPosY = nY-nYPos2*nScale
	local nSize = (nScale*nDotSize)*(radarsize/240)
	
	if((nXPos2*nScale)-((nSize/2)*(radarsize/240))<-(radarsize/2)) then return end
	if((nXPos2*nScale)+((nSize/2)*(radarsize/240))>(radarsize/2)) then return end
	if((nYPos2*nScale)-((nSize/2)*(radarsize/240))<-(radarsize/2)) then return end
	if((nYPos2*nScale)+((nSize/2)*(radarsize/240))>(radarsize/2)) then return end
	
	if (sIconName != "compass" && sIconName != "player") then
		local nXL1Pos2 = nXPos*nCosY-nZPos*nSinY
		local nZL1Pos2 = nZPos*nCosY+nXPos*nSinY
		local nYL1Pos2 = nYPos*nCosX-nZPos2*nSinX
		local nZL1Pos3 = nZPos2*nCosX+nYPos*nSinX
		local nYLPos = 0
		local nXL2Pos2 = nXPos*nCosY-nZPos*nSinY
		local nZL2Pos2 = nZPos*nCosY+nXPos*nSinY
		local nYL2Pos2 = nYLPos*nCosX-nZPos2*nSinX
		local nZL2Pos3 = nZPos2*nCosX+nYLPos*nSinX
		local nL1Scale = (nExp/(nExp+nZL1Pos3))*(radarsize/240)
		local nL1ScrPosX = nX+nXL1Pos2*nL1Scale
		local nL1ScrPosY = nY-nYL1Pos2*nL1Scale
		local nL2Scale = (nExp/(nExp+nZL2Pos3))*(radarsize/240)
		local nL2ScrPosX = nX+nXL2Pos2*nL2Scale
		local nL2ScrPosY = nY-nYL2Pos2*nL2Scale
		
		surface.SetDrawColor(255, 255, 255, 127)
		surface.DrawLine(nL1ScrPosX, nL1ScrPosY, nL2ScrPosX, nL2ScrPosY)
		surface.DrawLine(nL2ScrPosX-(nL2Scale*8), nL2ScrPosY, nL2ScrPosX+(nL2Scale*8), nL2ScrPosY)
	end
	
	draw.RoundedBox(0, nScrPosX-(nSize*nSizeOffs/2), nScrPosY-(nSize*nSizeOffs/2), nSize*nSizeOffs, nSize*nSizeOffs, cColour)
	

	if(GetConVarNumber("radar_showclasses") == 1) then
		if(sClassName == "compass") then return end
		if(sIconName == "player") then return end
		draw.SimpleText(getEntName(sClassName), "RadarClassNames", nScrPosX, nScrPosY-20, Color(220,220,255,255),1,1)
	end
end

function calculateCompass3D(nXPos, nYPos, nZPos, nXRot, nYRot, sAxis)
	local nExp = 220 -- Exposure, unknown unit
	local nDotSize = 3 -- Size of dots to draw
	local nMaxDist = 80
	local nXZDist = math.sqrt(nXPos*nXPos+nZPos*nZPos)
	if(nXZDist>nMaxDist && bRestrictRadius) then
		return
	end
	local nYRotRad = (nYRot-90)/180*math.pi
	local nCosY = math.cos(nYRotRad)
	local nSinY = math.sin(nYRotRad)
	local nXPos2 = nXPos*nCosY-nZPos*nSinY
	local nZPos2 = nZPos*nCosY+nXPos*nSinY
	local nXRotRad = nXRot/180*math.pi
	local nCosX = math.cos(nXRotRad)
	local nSinX = math.sin(nXRotRad)
	local nYPos2 = nYPos*nCosX-nZPos2*nSinX
	local nZPos3 = nZPos2*nCosX+nYPos*nSinX
	local nScale = (nExp/(nExp+nZPos3))*(radarsize/240)
	local nScrPosX = nX+nXPos2*nScale
	local nScrPosY = nY-nYPos2*nScale
	local nSize = (nScale*nDotSize)*(radarsize/240)
	if((nXPos2*nScale)-((nSize/2)*(radarsize/240))<-(radarsize/2)) then return end
	if((nXPos2*nScale)+((nSize/2)*(radarsize/240))>(radarsize/2)) then return end
	if((nYPos2*nScale)-((nSize/2)*(radarsize/240))<-(radarsize/2)) then return end
	if((nYPos2*nScale)+((nSize/2)*(radarsize/240))>(radarsize/2)) then return end
	draw.SimpleText(sAxis, "RadarCompass", nScrPosX, nScrPosY-20, Color(220,255,255,255),1,1)
end

function addEnt(sClass, cCol, nSizeOffs)
	local client = LocalPlayer()
	local clientpos = client:GetPos()
	local ent = ents.FindByClass(sClass)
	for k,v in pairs(ent) do
		local nNpcXPos = clientpos.x - v.Entity:GetPos().x
		local nNpcYPos = clientpos.z - v.Entity:GetPos().z
		local nNpcZPos = clientpos.y - v.Entity:GetPos().y
		local sClass = v.Entity:GetClass();
		local insert = {}
		local scale = 200
		insert[0] = nNpcXPos/scale
		insert[1] = -nNpcYPos/scale
		insert[2] = nNpcZPos/scale
		insert[3] = cCol
		insert[4] = true
		insert[5] = nSizeOffs
		insert[6] = sClass
		insert[7] = sClass
		
		table.insert(aPointsb, insert)
	end
end

function addPlayer(cCol, nSizeOffs)
	local client = LocalPlayer()
	local clientpos = client:GetPos()
	local players = player.GetAll()
	for k,v in pairs(players) do
		local nPlXPos = clientpos.x - v.Entity:GetPos().x
		local nPlYPos = clientpos.z - v.Entity:GetPos().z
		local nPlZPos = clientpos.y - v.Entity:GetPos().y
		local sName = v:Name()
		local insert = {}
		local scale = GetConVarNumber("radar_scale")
		insert[0] = nPlXPos/scale
		insert[1] = -nPlYPos/scale
		insert[2] = nPlZPos/scale
		insert[3] = cCol
		insert[4] = true
		insert[5] = nSizeOffs
		insert[6] = sName
		insert[7] = "player"
		table.insert(aPointsb, insert)
	end
end

Msg("Musik is from the Opensource game Vega Strike, Credit to the Orginal Authors")
local Soundlist = {}
Soundlist[1]="Freelancer/space_ambience.mp3"
Soundlist[2]="Freelancer/mission_complete.mp3"
Soundlist[3]="Freelancer/populated_station_dock.mp3"
Soundlist[4]="Freelancer/died.mp3"
Soundlist[5]="Freelancer/ship_largescale_battle.mp3"
Soundlist[6]="Freelancer/ship_medium_battle.mp3"
Soundlist[7]="Freelancer/defencesystem_battle.mp3"
Soundlist[8]="Freelancer/mine_field.mp3"
Soundlist[9]="Freelancer/ship_small_battle.mp3"
Soundlist[10]="Freelancer/tradestation_ambience.mp3"
Soundlist[11]="Null"
for k, Sound in pairs (Soundlist) do
	util.PrecacheSound( Sound )
end
local playtime = {128,6.5,26,29,62,71,81,88,102,113,999,24,999}
local trackplayed = 11
local trackend = 10
local tick = CurTime()
local swich = true
Player.playonlyonce = 0

function Musiccontroller()
	if (CurTime() >= trackend) then
		Msg("Trackended \n")
		LocalPlayer():ConCommand("AcualizeMusic")
		trackplayed = 0
		Player.playonlyonce = 0
	end
	if (tracktoplay) then
		if (Player.playonlyonce != 2) then
			if (trackplayed != tracktoplay) then
				Msg (" Playnewsound"..Soundlist[tracktoplay] .. "\n")
				LocalPlayer():ConCommand("stopsounds")	
				if (Player.playonlyonce == 1) then
					Player.playonlyonce = 2
				end
				timer.Create("Musictimer",0.4,1,Playfile,tracktoplay) 
				trackplayed = tracktoplay
				trackend = trackend + 10
				tracktoplay = nil
			end
		end
	end
end
hook.Add("Tick", "Musiccontroller", Musiccontroller)

function Playfile(musicenumeration)
	LocalPlayer():EmitSound(Soundlist[musicenumeration],80,100)
	trackend = CurTime() + playtime[musicenumeration]
end

function MusicPlay(ply,command,tracktoplay2) 
	tracktoplay = tonumber( tracktoplay2[1] )
	Msg(tracktoplay)
	if tracktoplay2[2] then
		Player.playonlyonce = 1
	end
end 
concommand.Add( "ChangeTrack", MusicPlay ) 

DeriveGamemode( "sandbox" )
GM.IsSandboxDerived = true

function Invehicle( um )
    Player.InVehicle2 = um:ReadBool()
	Player.shut = um:ReadEntity()
end 
usermessage.Hook("InVehicle", Invehicle) 

function CapsuleView( ply, origin, angles, fov )
	if (Player.InVehicle2  && Player.shut:IsValid() ) then
		local view = {}
		view.origin = Player.shut:GetPos() + LocalPlayer():GetUp( )*50 + LocalPlayer():GetAimVector():GetNormal()* 50
		view.angles = angles
		return view
	end
end
hook.Add("CalcView", "CapsuleView", CapsuleView)
